home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1W8CCC4 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  14.7 KB  |  537 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.swing.event.EventListenerList;
  6. import com.sun.java.swing.event.ListSelectionEvent;
  7. import com.sun.java.swing.event.ListSelectionListener;
  8. import com.sun.java.swing.plaf.ListUI;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.awt.Container;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14. import java.awt.Insets;
  15. import java.awt.Point;
  16. import java.awt.Rectangle;
  17. import java.util.Vector;
  18.  
  19. public class JList extends JComponent implements Scrollable, Accessible {
  20.    private int fixedCellWidth;
  21.    private int fixedCellHeight;
  22.    private Object prototypeCellValue;
  23.    private int visibleRowCount;
  24.    private Color selectionForeground;
  25.    private Color selectionBackground;
  26.    private ListSelectionModel selectionModel;
  27.    private ListModel dataModel;
  28.    private ListCellRenderer cellRenderer;
  29.    private ListSelectionListener selectionListener;
  30.    static Class class$com$sun$java$swing$event$ListSelectionListener;
  31.  
  32.    public JList() {
  33.       this((ListModel)(new 1()));
  34.    }
  35.  
  36.    public JList(Object[] listData) {
  37.       this((ListModel)(new 2(listData)));
  38.    }
  39.  
  40.    public JList(ListModel dataModel) {
  41.       this.fixedCellWidth = -1;
  42.       this.fixedCellHeight = -1;
  43.       this.visibleRowCount = 8;
  44.       if (dataModel == null) {
  45.          throw new IllegalArgumentException("dataModel must be non null");
  46.       } else {
  47.          this.dataModel = dataModel;
  48.          this.selectionModel = this.createSelectionModel();
  49.          ((JComponent)this).setAutoscrolls(true);
  50.          this.updateUI();
  51.       }
  52.    }
  53.  
  54.    public JList(Vector listData) {
  55.       this((ListModel)(new 3(listData)));
  56.    }
  57.  
  58.    public void addListSelectionListener(ListSelectionListener listener) {
  59.       if (this.selectionListener == null) {
  60.          this.selectionListener = new 4(this);
  61.          this.getSelectionModel().addListSelectionListener(this.selectionListener);
  62.       }
  63.  
  64.       EventListenerList var10000 = super.listenerList;
  65.       Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
  66.       if (var10001 == null) {
  67.          try {
  68.             var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
  69.          } catch (ClassNotFoundException var2) {
  70.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  71.          }
  72.  
  73.          class$com$sun$java$swing$event$ListSelectionListener = var10001;
  74.       }
  75.  
  76.       var10000.add(var10001, listener);
  77.    }
  78.  
  79.    public void addSelectionInterval(int anchor, int lead) {
  80.       this.getSelectionModel().addSelectionInterval(anchor, lead);
  81.    }
  82.  
  83.    public void clearSelection() {
  84.       this.getSelectionModel().clearSelection();
  85.    }
  86.  
  87.    protected ListSelectionModel createSelectionModel() {
  88.       return new DefaultListSelectionModel();
  89.    }
  90.  
  91.    public void ensureIndexIsVisible(int index) {
  92.       Rectangle cellBounds = this.getCellBounds(index, index);
  93.       if (cellBounds != null) {
  94.          ((JComponent)this).scrollRectToVisible(cellBounds);
  95.       }
  96.  
  97.    }
  98.  
  99.    protected void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) {
  100.       Object[] listeners = super.listenerList.getListenerList();
  101.       ListSelectionEvent e = null;
  102.  
  103.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  104.          Object var10000 = listeners[i];
  105.          Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
  106.          if (var10001 == null) {
  107.             try {
  108.                var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
  109.             } catch (ClassNotFoundException var7) {
  110.                throw new NoClassDefFoundError(((Throwable)var7).getMessage());
  111.             }
  112.  
  113.             class$com$sun$java$swing$event$ListSelectionListener = var10001;
  114.          }
  115.  
  116.          if (var10000 == var10001) {
  117.             if (e == null) {
  118.                e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
  119.             }
  120.  
  121.             ((ListSelectionListener)listeners[i + 1]).valueChanged(e);
  122.          }
  123.       }
  124.  
  125.    }
  126.  
  127.    public AccessibleContext getAccessibleContext() {
  128.       if (super.accessibleContext == null) {
  129.          super.accessibleContext = new AccessibleJList(this);
  130.       }
  131.  
  132.       return super.accessibleContext;
  133.    }
  134.  
  135.    public int getAnchorSelectionIndex() {
  136.       return this.getSelectionModel().getAnchorSelectionIndex();
  137.    }
  138.  
  139.    public Rectangle getCellBounds(int index1, int index2) {
  140.       ListUI ui = this.getUI();
  141.       return ui != null ? ui.getCellBounds(this, index1, index2) : null;
  142.    }
  143.  
  144.    public ListCellRenderer getCellRenderer() {
  145.       return this.cellRenderer;
  146.    }
  147.  
  148.    public int getFirstVisibleIndex() {
  149.       Point visibleUL = ((JComponent)this).getVisibleRect().getLocation();
  150.       return this.locationToIndex(visibleUL);
  151.    }
  152.  
  153.    public int getFixedCellHeight() {
  154.       return this.fixedCellHeight;
  155.    }
  156.  
  157.    public int getFixedCellWidth() {
  158.       return this.fixedCellWidth;
  159.    }
  160.  
  161.    public int getLastVisibleIndex() {
  162.       Rectangle r = ((JComponent)this).getVisibleRect();
  163.       Point visibleLR = new Point(r.x + r.width - 1, r.y + r.height - 1);
  164.       return this.locationToIndex(visibleLR);
  165.    }
  166.  
  167.    public int getLeadSelectionIndex() {
  168.       return this.getSelectionModel().getLeadSelectionIndex();
  169.    }
  170.  
  171.    public int getMaxSelectionIndex() {
  172.       return this.getSelectionModel().getMaxSelectionIndex();
  173.    }
  174.  
  175.    public int getMinSelectionIndex() {
  176.       return this.getSelectionModel().getMinSelectionIndex();
  177.    }
  178.  
  179.    public ListModel getModel() {
  180.       return this.dataModel;
  181.    }
  182.  
  183.    public Dimension getPreferredScrollableViewportSize() {
  184.       Insets insets = ((JComponent)this).getInsets();
  185.       int dx = insets.left + insets.right;
  186.       int dy = insets.top + insets.bottom;
  187.       int visibleRowCount = this.getVisibleRowCount();
  188.       int fixedCellWidth = this.getFixedCellWidth();
  189.       int fixedCellHeight = this.getFixedCellHeight();
  190.       if (fixedCellWidth > 0 && fixedCellHeight > 0) {
  191.          int width = fixedCellWidth + dx;
  192.          int height = visibleRowCount * fixedCellHeight + dy;
  193.          return new Dimension(width, height);
  194.       } else if (this.getModel().getSize() > 0) {
  195.          int width = ((JComponent)this).getPreferredSize().width;
  196.          Rectangle r = this.getCellBounds(0, 0);
  197.          int height = visibleRowCount * r.height + dy;
  198.          return new Dimension(width, height);
  199.       } else {
  200.          fixedCellWidth = fixedCellWidth > 0 ? fixedCellWidth : 256;
  201.          fixedCellHeight = fixedCellHeight > 0 ? fixedCellHeight : 16;
  202.          return new Dimension(fixedCellWidth, fixedCellHeight * visibleRowCount);
  203.       }
  204.    }
  205.  
  206.    public Object getPrototypeCellValue() {
  207.       return this.prototypeCellValue;
  208.    }
  209.  
  210.    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
  211.       return orientation == 1 ? visibleRect.height : visibleRect.width;
  212.    }
  213.  
  214.    public boolean getScrollableTracksViewportHeight() {
  215.       return false;
  216.    }
  217.  
  218.    public boolean getScrollableTracksViewportWidth() {
  219.       return false;
  220.    }
  221.  
  222.    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
  223.       if (orientation == 0) {
  224.          return 1;
  225.       } else {
  226.          int row = this.getFirstVisibleIndex();
  227.          if (row == -1) {
  228.             return 0;
  229.          } else if (direction > 0) {
  230.             Rectangle r = this.getCellBounds(row, row);
  231.             return r == null ? 0 : r.height - (visibleRect.y - r.y);
  232.          } else {
  233.             Rectangle r = this.getCellBounds(row, row);
  234.             if (r.y == visibleRect.y && row == 0) {
  235.                return 0;
  236.             } else if (r.y == visibleRect.y) {
  237.                Rectangle prevR = this.getCellBounds(row - 1, row - 1);
  238.                return prevR == null ? 0 : prevR.height;
  239.             } else {
  240.                return visibleRect.y - r.y;
  241.             }
  242.          }
  243.       }
  244.    }
  245.  
  246.    public int getSelectedIndex() {
  247.       return this.getMinSelectionIndex();
  248.    }
  249.  
  250.    public int[] getSelectedIndices() {
  251.       ListSelectionModel sm = this.getSelectionModel();
  252.       int iMin = sm.getMinSelectionIndex();
  253.       int iMax = sm.getMaxSelectionIndex();
  254.       if (iMin >= 0 && iMax >= 0) {
  255.          int[] rvTmp = new int[1 + (iMax - iMin)];
  256.          int n = 0;
  257.  
  258.          for(int i = iMin; i <= iMax; ++i) {
  259.             if (sm.isSelectedIndex(i)) {
  260.                rvTmp[n++] = i;
  261.             }
  262.          }
  263.  
  264.          int[] rv = new int[n];
  265.          System.arraycopy(rvTmp, 0, rv, 0, n);
  266.          return rv;
  267.       } else {
  268.          return new int[0];
  269.       }
  270.    }
  271.  
  272.    public Object getSelectedValue() {
  273.       int i = this.getMinSelectionIndex();
  274.       return i == -1 ? null : this.getModel().getElementAt(i);
  275.    }
  276.  
  277.    public Object[] getSelectedValues() {
  278.       ListSelectionModel sm = this.getSelectionModel();
  279.       ListModel dm = this.getModel();
  280.       int iMin = sm.getMinSelectionIndex();
  281.       int iMax = sm.getMaxSelectionIndex();
  282.       if (iMin >= 0 && iMax >= 0) {
  283.          Object[] rvTmp = new Object[1 + (iMax - iMin)];
  284.          int n = 0;
  285.  
  286.          for(int i = iMin; i <= iMax; ++i) {
  287.             if (sm.isSelectedIndex(i)) {
  288.                rvTmp[n++] = dm.getElementAt(i);
  289.             }
  290.          }
  291.  
  292.          Object[] rv = new Object[n];
  293.          System.arraycopy(rvTmp, 0, rv, 0, n);
  294.          return rv;
  295.       } else {
  296.          return new Object[0];
  297.       }
  298.    }
  299.  
  300.    public Color getSelectionBackground() {
  301.       return this.selectionBackground;
  302.    }
  303.  
  304.    public Color getSelectionForeground() {
  305.       return this.selectionForeground;
  306.    }
  307.  
  308.    public int getSelectionMode() {
  309.       return this.getSelectionModel().getSelectionMode();
  310.    }
  311.  
  312.    public ListSelectionModel getSelectionModel() {
  313.       return this.selectionModel;
  314.    }
  315.  
  316.    public ListUI getUI() {
  317.       return (ListUI)super.ui;
  318.    }
  319.  
  320.    public String getUIClassID() {
  321.       return "ListUI";
  322.    }
  323.  
  324.    public boolean getValueIsAdjusting() {
  325.       return this.getSelectionModel().getValueIsAdjusting();
  326.    }
  327.  
  328.    public int getVisibleRowCount() {
  329.       return this.visibleRowCount;
  330.    }
  331.  
  332.    public Point indexToLocation(int index) {
  333.       ListUI ui = this.getUI();
  334.       return ui != null ? ui.indexToLocation(this, index) : null;
  335.    }
  336.  
  337.    public boolean isOpaque() {
  338.       return true;
  339.    }
  340.  
  341.    public boolean isSelectedIndex(int index) {
  342.       return this.getSelectionModel().isSelectedIndex(index);
  343.    }
  344.  
  345.    public boolean isSelectionEmpty() {
  346.       return this.getSelectionModel().isSelectionEmpty();
  347.    }
  348.  
  349.    public int locationToIndex(Point location) {
  350.       ListUI ui = this.getUI();
  351.       return ui != null ? ui.locationToIndex(this, location) : -1;
  352.    }
  353.  
  354.    public void removeListSelectionListener(ListSelectionListener listener) {
  355.       EventListenerList var10000 = super.listenerList;
  356.       Class var10001 = class$com$sun$java$swing$event$ListSelectionListener;
  357.       if (var10001 == null) {
  358.          try {
  359.             var10001 = Class.forName("com.sun.java.swing.event.ListSelectionListener");
  360.          } catch (ClassNotFoundException var2) {
  361.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  362.          }
  363.  
  364.          class$com$sun$java$swing$event$ListSelectionListener = var10001;
  365.       }
  366.  
  367.       var10000.remove(var10001, listener);
  368.    }
  369.  
  370.    public void removeSelectionInterval(int index0, int index1) {
  371.       this.getSelectionModel().removeSelectionInterval(index0, index1);
  372.    }
  373.  
  374.    public void setCellRenderer(ListCellRenderer cellRenderer) {
  375.       ListCellRenderer oldValue = this.cellRenderer;
  376.       this.cellRenderer = cellRenderer;
  377.       if (cellRenderer != null && !cellRenderer.equals(oldValue)) {
  378.          this.updateFixedCellSize();
  379.       }
  380.  
  381.       ((JComponent)this).firePropertyChange("cellRenderer", oldValue, cellRenderer);
  382.    }
  383.  
  384.    public void setFixedCellHeight(int height) {
  385.       int oldValue = this.fixedCellHeight;
  386.       this.fixedCellHeight = height;
  387.       ((JComponent)this).firePropertyChange("fixedCellHeight", oldValue, this.fixedCellHeight);
  388.    }
  389.  
  390.    public void setFixedCellWidth(int width) {
  391.       int oldValue = this.fixedCellWidth;
  392.       this.fixedCellWidth = width;
  393.       ((JComponent)this).firePropertyChange("fixedCellWidth", oldValue, this.fixedCellWidth);
  394.    }
  395.  
  396.    public void setListData(Object[] listData) {
  397.       this.setModel(new 5(listData));
  398.    }
  399.  
  400.    public void setListData(Vector listData) {
  401.       this.setModel(new 6(listData));
  402.    }
  403.  
  404.    public void setModel(ListModel model) {
  405.       ListModel oldValue = this.dataModel;
  406.       this.dataModel = model;
  407.       ((JComponent)this).firePropertyChange("model", oldValue, this.dataModel);
  408.       this.clearSelection();
  409.    }
  410.  
  411.    public void setPrototypeCellValue(Object prototypeCellValue) {
  412.       Object oldValue = this.prototypeCellValue;
  413.       this.prototypeCellValue = prototypeCellValue;
  414.       if (prototypeCellValue != null && !prototypeCellValue.equals(oldValue)) {
  415.          this.updateFixedCellSize();
  416.       }
  417.  
  418.       ((JComponent)this).firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue);
  419.    }
  420.  
  421.    public void setSelectedIndex(int index) {
  422.       this.getSelectionModel().setSelectionInterval(index, index);
  423.    }
  424.  
  425.    public void setSelectedIndices(int[] indices) {
  426.       ListSelectionModel sm = this.getSelectionModel();
  427.       sm.clearSelection();
  428.  
  429.       for(int i = 0; i < indices.length; ++i) {
  430.          sm.addSelectionInterval(indices[i], indices[i]);
  431.       }
  432.  
  433.    }
  434.  
  435.    public void setSelectedValue(Object anObject, boolean shouldScroll) {
  436.       if (anObject == null) {
  437.          this.setSelectedIndex(-1);
  438.       } else if (!anObject.equals(this.getSelectedValue())) {
  439.          ListModel dm = this.getModel();
  440.          int i = 0;
  441.  
  442.          for(int c = dm.getSize(); i < c; ++i) {
  443.             if (anObject.equals(dm.getElementAt(i))) {
  444.                this.setSelectedIndex(i);
  445.                if (shouldScroll) {
  446.                   this.ensureIndexIsVisible(i);
  447.                }
  448.  
  449.                ((Component)this).repaint();
  450.                return;
  451.             }
  452.          }
  453.  
  454.          this.setSelectedIndex(-1);
  455.       }
  456.  
  457.       ((Component)this).repaint();
  458.    }
  459.  
  460.    public void setSelectionBackground(Color selectionBackground) {
  461.       Color oldValue = this.selectionBackground;
  462.       this.selectionBackground = selectionBackground;
  463.       ((JComponent)this).firePropertyChange("selectionBackground", oldValue, selectionBackground);
  464.    }
  465.  
  466.    public void setSelectionForeground(Color selectionForeground) {
  467.       Color oldValue = this.selectionForeground;
  468.       this.selectionForeground = selectionForeground;
  469.       ((JComponent)this).firePropertyChange("selectionForeground", oldValue, selectionForeground);
  470.    }
  471.  
  472.    public void setSelectionInterval(int anchor, int lead) {
  473.       this.getSelectionModel().setSelectionInterval(anchor, lead);
  474.    }
  475.  
  476.    public void setSelectionMode(int selectionMode) {
  477.       this.getSelectionModel().setSelectionMode(selectionMode);
  478.    }
  479.  
  480.    public void setSelectionModel(ListSelectionModel selectionModel) {
  481.       if (selectionModel == null) {
  482.          throw new IllegalArgumentException("selectionModel must be non null");
  483.       } else {
  484.          if (this.selectionListener != null) {
  485.             this.selectionModel.removeListSelectionListener(this.selectionListener);
  486.             selectionModel.addListSelectionListener(this.selectionListener);
  487.          }
  488.  
  489.          ListSelectionModel oldValue = this.selectionModel;
  490.          this.selectionModel = selectionModel;
  491.          ((JComponent)this).firePropertyChange("selectionModel", oldValue, selectionModel);
  492.          if (super.accessibleContext != null) {
  493.             super.accessibleContext.firePropertyChange("AccessibleSelection", new Boolean(false), new Boolean(true));
  494.          }
  495.  
  496.       }
  497.    }
  498.  
  499.    public void setUI(ListUI ui) {
  500.       super.setUI(ui);
  501.    }
  502.  
  503.    public void setValueIsAdjusting(boolean b) {
  504.       this.getSelectionModel().setValueIsAdjusting(b);
  505.    }
  506.  
  507.    public void setVisibleRowCount(int visibleRowCount) {
  508.       int oldValue = this.visibleRowCount;
  509.       this.visibleRowCount = Math.max(0, visibleRowCount);
  510.       ((JComponent)this).firePropertyChange("visibleRowCount", oldValue, visibleRowCount);
  511.    }
  512.  
  513.    private void updateFixedCellSize() {
  514.       ListCellRenderer cr = this.getCellRenderer();
  515.       Object value = this.getPrototypeCellValue();
  516.       if (cr != null && value != null) {
  517.          Component c = cr.getListCellRendererComponent(this, value, 0, false, false);
  518.          Font f = c.getFont();
  519.          c.setFont(((Component)this).getFont());
  520.          Dimension d = c.getPreferredSize();
  521.          this.fixedCellWidth = d.width;
  522.          this.fixedCellHeight = d.height;
  523.          c.setFont(f);
  524.       }
  525.  
  526.    }
  527.  
  528.    public void updateUI() {
  529.       this.setUI((ListUI)UIManager.getUI(this));
  530.       ((Container)this).invalidate();
  531.    }
  532.  
  533.    static ListSelectionModel access$selectionModel(JList var0) {
  534.       return var0.selectionModel;
  535.    }
  536. }
  537.